Guice হলো Google-এর একটি Lightweight Dependency Injection Framework। এটি Java অ্যাপ্লিকেশনের জন্য Dependency Management সহজ এবং কার্যকর করে তোলে। Guice ব্যবহার শুরু করতে হলে প্রয়োজনীয় JAR ফাইল এবং ডিপেন্ডেন্সি কনফিগারেশন সঠিকভাবে সেটআপ করতে হবে।
Guice এর জন্য প্রয়োজনীয় JAR ফাইল
Guice ব্যবহার করতে নিচের JAR ফাইলগুলো প্রয়োজন হতে পারে:
- guice-x.x.x.jar (মূল Guice লাইব্রেরি)
- javax.inject.jar (Java Injection API, Guice এ ব্যবহৃত হয়)
- aopalliance.jar (AOP সাপোর্টের জন্য)
Dependency Configuration (Maven)
Guice ব্যবহার করার জন্য Maven প্রজেক্টে নিচের ডিপেন্ডেন্সি যোগ করুন:
Maven Dependency:
<dependencies>
<!-- Guice Core -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>
<!-- javax.inject API -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- AOP Alliance (optional, if using AOP features) -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Dependency Configuration (Gradle)
Guice ব্যবহার করতে হলে Gradle প্রজেক্টে নিচের ডিপেন্ডেন্সি যোগ করুন:
Gradle Dependency:
dependencies {
// Guice Core
implementation 'com.google.inject:guice:5.1.0'
// javax.inject API
implementation 'javax.inject:javax.inject:1'
// AOP Alliance (optional, if needed)
implementation 'aopalliance:aopalliance:1.0'
}
Manual JAR File Configuration
যদি আপনি Maven বা Gradle ব্যবহার না করেন:
- Guice JAR ফাইল ডাউনলোড করুন Maven Central বা Google Guice Project থেকে।
- JAR ফাইলগুলো আপনার প্রজেক্টের
libsফোল্ডারে যোগ করুন। - Classpath-এ JAR ফাইলগুলো যোগ করুন।
Guice Quick Example
1. Basic Guice Module এবং Injection
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
// Service Interface
interface GreetingService {
void greet(String name);
}
// Service Implementation
class GreetingServiceImpl implements GreetingService {
public void greet(String name) {
System.out.println("Hello, " + name);
}
}
// Guice Module
class AppModule extends AbstractModule {
@Override
protected void configure() {
bind(GreetingService.class).to(GreetingServiceImpl.class);
}
}
// Application Class
public class GuiceExample {
private final GreetingService greetingService;
@Inject
public GuiceExample(GreetingService greetingService) {
this.greetingService = greetingService;
}
public void run() {
greetingService.greet("World");
}
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AppModule());
GuiceExample app = injector.getInstance(GuiceExample.class);
app.run();
}
}
2. AOP Example (Optional)
AOP ব্যবহার করতে হলে aopalliance.jar যোগ করুন এবং Guice এর MethodInterceptor ব্যবহার করুন।
Dependency Version Compatibility
- Guice Core: সর্বশেষ সংস্করণ
5.1.0(অথবা উপযুক্ত আপডেট) - javax.inject: সর্বশেষ সংস্করণ
1 - AOP Alliance: সর্বশেষ সংস্করণ
1.0
Guice সেটআপ করার জন্য Maven, Gradle, অথবা ম্যানুয়াল JAR ফাইল ব্যবহারের মাধ্যমে সঠিকভাবে ডিপেন্ডেন্সি কনফিগার করুন। Guice আপনাকে Dependency Injection এর মাধ্যমে কোড আরও ক্লিন, মডুলার, এবং Maintainable করতে সাহায্য করবে।
Read more